//
//  FindViewController.m
//  UISegmentControl_test
//
//  Created by Mac on 2020/12/1.
//  Copyright © 2020年 Lorisyy. All rights reserved.
//

#import "FindViewController.h"

@interface FindViewController () <UITableViewDelegate, UISearchBarDelegate, CAAnimationDelegate, UITableViewDataSource>{
    NSMutableArray *datas; // 总数据
    UITableView *tableView; //表单视图
    CommentViewController *page; //内容页
}
@property (assign,nonatomic) int pageIndex;
@end

@implementation FindViewController

- (void)addCard:(SingleCard *)card{
    [datas addObject:card];
    // 实现按时间排序
    [datas sortUsingComparator:^NSComparisonResult(SingleCard *c1, SingleCard *c2) {
        NSComparisonResult result = [[c1 getTime] compare:[c2 getTime]];
        return result;
    }];
//    [tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 50, 390, 790) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tableFooterView = [UIView new];
    tableView.backgroundColor = [UIColor clearColor]; // 设置成透明
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:tableView];
    // Do any additional setup after loading the view.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
// 选中某行后的效果(转到评论区)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (page == nil) {
        page = [[CommentViewController alloc]init];
        page.view.frame = [UIScreen mainScreen].bounds;
        [page loadView];
        [page viewDidLoad];
    }
//     Return Form Detail
    CATransition *tran = [CATransition animation];
    tran.delegate = self;
    tran.type = @"push";
    tran.subtype=kCATransitionFromRight;
    [self.navigationController.view.layer addAnimation:tran forKey:nil];
    [self.navigationController pushViewController:page animated:YES];
}


- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return datas.count;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.textLabel.text = [datas[indexPath.row] getName];
    cell.layer.borderColor = [UIColor blackColor].CGColor;
    cell.layer.borderWidth = 2.0;
    cell.layer.cornerRadius = 10.0;  // 圆角
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.text = [[NSString alloc] initWithFormat:@"Name：        %@\nDate：        %@\nTag：          %@\nContext：%@\n Likes：          %d\n", [datas[indexPath.row] getName], [datas[indexPath.row] getTime], [datas[indexPath.row] getTags], [datas[indexPath.row] getContent],[datas[indexPath.row] getLikesNum]];
    cell.backgroundColor = [UIColor clearColor]; // 设置成透明能显示背景渐变色
    
    // CGFloat height = cell
    UILabel *likeLabel = [[UILabel alloc] initWithFrame:CGRectMake(321 , 130, 50, 20)];
    likeLabel.text = [NSString stringWithFormat:@"%d",[datas[indexPath.row] getLikesNum]];
    likeLabel.numberOfLines = 0;
    likeLabel.textColor = [UIColor blackColor];
    likeLabel.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:likeLabel];
    
    UIButton  *likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    likeButton.frame = CGRectMake(301 , 130, 20, 20);
    likeButton.tag =[datas[indexPath.row] getLikesNum];
    [likeButton setImage:[UIImage imageNamed:@"like.png"] forState:UIControlStateNormal];
    [likeButton addTarget:self action:@selector(likeButtonClicked:) forControlEvents:UIControlEventTouchDown];
    likeButton.tag = indexPath.row;
    [cell.contentView addSubview:likeButton];
    
    
    UILabel *comLabel = [[UILabel alloc] initWithFrame:CGRectMake(171 , 130, 90, 20)];
    comLabel.text = [NSString stringWithFormat:@"%d",[datas[indexPath.row] getCommentsNum]];
    comLabel.numberOfLines = 0;
    comLabel.textColor = [UIColor blackColor];
    comLabel.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:comLabel];
    
    UIButton  *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    commentButton.frame = CGRectMake(151 , 130, 20, 20);
    commentButton.tag =[datas[indexPath.row] getLikesNum];
    [commentButton setImage:[UIImage imageNamed:@"comment.png"] forState:UIControlStateNormal];
    [commentButton addTarget:self action:@selector(commentButtonClicked:) forControlEvents:UIControlEventTouchDown];
    commentButton.tag = indexPath.row;
    [cell.contentView addSubview:commentButton];
    
    UILabel *starLabel = [[UILabel alloc] initWithFrame:CGRectMake(21 , 130, 50, 20)];
    starLabel.text = @"Star";
    starLabel.numberOfLines = 0;
    starLabel.textColor = [UIColor blackColor];
    starLabel.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:starLabel];
    
    UIButton  *starButton = [UIButton buttonWithType:UIButtonTypeCustom];
    starButton.frame = CGRectMake(1 , 130, 20, 20);
    starButton.tag =[datas[indexPath.row] getLikesNum];
    [starButton setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
    [starButton addTarget:self action:@selector(starButtonClicked:) forControlEvents:UIControlEventTouchDown];
    starButton.tag = indexPath.row;
    [cell.contentView addSubview:starButton];
    return cell;
}

- (void)likeButtonClicked:(nonnull NSIndexPath *)indexPath{
    int n =[datas[indexPath.row] getLikesNum];
    n = n+1;
    NSLog(@"Like = %d ++",n);
    [datas[indexPath.row] setLikesNum:n];
    
    return;
}


- (BOOL)shouldUpdateFocusInContext:(nonnull UIFocusUpdateContext *)context {
    return true;
}
- (instancetype) init{
    if (self == nil){
        self =  [super init];
    }
    _pageIndex = 0;
    self.navigationItem.title = @"广场";
    [self updateSource];
    return self;
}

-(void)updateSource{
    NSLog(@"page:%d",_pageIndex);
    if (datas == nil) {
        datas = [[NSMutableArray alloc] init];
    }
    _pageIndex++;
    ContentRequest *server = [ContentRequest defaultContentRequest];
    [server getPublicContents:_pageIndex eachPage:5 success:^(NSArray<Content *> *contents) {
        Content *c;
        for (c in contents) {
            SingleCard *card = [[SingleCard alloc]init];
            [card setName:[[c text]title]];
            NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:[c EditDate]/1000];
            [card setTime:[NSString stringWithFormat:@"%@",date]];
            //            [card setTags:[[c text]tags]];
            [card setContent:[[c text]detail]];
            [self->datas addObject:card];
        }
        [self->tableView reloadData];
    } failure:^(NSString *reason) {
        NSLog(@"%@",reason);
    }];
    
//    // 初始数据：
//    // 亚利桑那州·菲尼克斯和宾夕法尼亚州·费城
//    SingleCard *card1 = [[SingleCard alloc] init]; SingleCard *card2 = [[SingleCard alloc] init];
//    [card1 setName:@"Lorisyy"]; [card1 setTime:@"2020-09-09"]; [card1 setTags:@"#Philadelphia"]; [card1 setContent:@"Hello there"];
//    [card2 setName:@"Lorisyy"]; [card2 setTime:@"2020-10-10"]; [card2 setTags:@"#Guangzhou"]; [card2 setContent:@"In SYSU"];
//    [datas addObject:card1];
//    [datas addObject:card2];
    // 按时间排序
    [datas sortUsingComparator:^NSComparisonResult(SingleCard *c1, SingleCard *c2) {
        NSComparisonResult result = [[c1 getTime] compare:[c2 getTime]];
        return result;
    }];
}

- (void)scrollViewDidScroll:(UIScrollView*)scrollView{
    if(scrollView.contentSize.height-640<=scrollView.contentOffset.y){
        [self updateSource];
    }
}

@end
